/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.Userexercisesetlog;
import me.zhengjie.modules.system.domain.Userquestionlog;
import me.zhengjie.modules.system.domain.vo.UserquestionlogQueryCriteria;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.mapper.UserquestionlogMapper;
import me.zhengjie.modules.system.service.UserquestionlogService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author shitou
* @date 2024-05-29
**/
@Service
@RequiredArgsConstructor
public class UserquestionlogServiceImpl extends ServiceImpl<UserquestionlogMapper, Userquestionlog> implements UserquestionlogService {

    private final UserquestionlogMapper userquestionlogMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Userquestionlog> queryAll(UserquestionlogQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(userquestionlogMapper.findAll(criteria, page));
    }

    @Override
    public List<Userquestionlog> queryAll(UserquestionlogQueryCriteria criteria){
        return userquestionlogMapper.findAll(criteria);
    }

    public List<Userquestionlog> queryUserlist(QueryWrapper queryWrapper){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        queryWrapper.eq("userId",currentUser.getId());
        return userquestionlogMapper.selectList(queryWrapper);
    }


    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Userquestionlog resources) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        resources.setUserid(currentUser.getId());

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teachId",resources.getTeachid());

        queryWrapper.eq("teachChapterId",resources.getTeachchapterid());
        queryWrapper.eq("teachSubsectionId",resources.getTeachsubsectionid());
        if(resources.getSubsectionpluginsid()!=null){

            queryWrapper.eq("subsectionExerciseSetId",resources.getSubsectionpluginsid());
        }
        if(resources.getCourseid()!=null){

            queryWrapper.eq("courseId",resources.getCourseid());
        }
        if(resources.getExercisesetid()!=null){

            queryWrapper.eq("exerciseSetId",resources.getExercisesetid());
        }
        if(resources.getType()!=null){

            queryWrapper.eq("type",resources.getType());
        }
        if(resources.getQuestiontype()!=null){

            queryWrapper.eq("questionType",resources.getQuestiontype());
        }
        if(resources.getQuestionid()!=null){

            queryWrapper.eq("questionId",resources.getQuestionid());
        }
        if(resources.getUserid()!=null){
            queryWrapper.eq("userId",resources.getUserid());
        } else {
            throw new Exception("尚未登录");
        }



        if(userquestionlogMapper.selectCount(queryWrapper)>0){

            Userquestionlog userquestionlog = userquestionlogMapper.selectOne(queryWrapper);
            userquestionlog.copy(resources);
            saveOrUpdate(userquestionlog);
        } else {
            save(resources);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Userquestionlog resources) {
        Userquestionlog userquestionlog = getById(resources.getId());
        userquestionlog.copy(resources);
        saveOrUpdate(userquestionlog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Userquestionlog> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Userquestionlog userquestionlog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("教材id", userquestionlog.getTeachid());
            map.put("教材章节id", userquestionlog.getTeachchapterid());
            map.put("教材小节id", userquestionlog.getTeachsubsectionid());
            map.put("小节组件id", userquestionlog.getSubsectionpluginsid());
            map.put("习题集id", userquestionlog.getExercisesetid());
            map.put("是否正确", userquestionlog.getIstrue());
            map.put("答题内容", userquestionlog.getContent());
            map.put(" lastupdatetime",  userquestionlog.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}